import _ from 'lodash'
import axios from '@/utils/axios'
import { domainData } from 'api'

const UI = {
  DROPDOWN: 'DROPDOWN',
  PREVIEW: 'PREVIEW'
}

const initialState = {
  dropdown: [],
  preview: {
    show: false,
    url: ''
  }
}

const mutations = {
  [UI.DROPDOWN] (state, payload) {
    state.dropdown = payload
  },
  [UI.PREVIEW] (state, payload) {
    state.preview = {
      ...state.preview,
      ...payload
    }
  }
}

const actions = {
  async getDropdownList ({ commit, dispatch }, payload) {
    try {
      const { ResultSet } = await axios.post(domainData, { action: 'PlatCommonProcess', subaction: 'domainData', content: { loadOnStartup: true, organizationId:1}})
      dispatch('changeDropdown', ResultSet.Result)
    } catch (err) {
      console.log(err)
    }
  },
  changeDropdown ({ commit }, payload) {
    commit(UI.DROPDOWN, payload)
  },
  getDropdown ({ state }, { key }) {
    if (state.dropdown.length) {
      return state.dropdown.filter(item => {
        return item.tableName === key
      })
    }
  },
  changePreviewModal ({ commit }, payload) {
    commit(UI.PREVIEW, payload)
  }
}

const getters = {}

export default {
  state: _.cloneDeep(initialState),
  mutations,
  actions,
  getters
}
